package com.kly.chatter.api.application.task;


import cn.hutool.core.net.NetUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.fqgj.common.utils.CollectionUtils;
import com.jsoniter.JsonIterator;
import com.jsoniter.any.Any;
import com.jsoniter.output.JsonStream;
import com.kly.chatter.db.dao.ChatterOnlineListMatchRecordDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 该任务用于定时重置主播的在线列表的额度
 */
@Component
@Slf4j
public class RefreshOnlineListLimitationTask {

    @NacosValue(value = "${refresh.online.list.mins:60}", autoRefreshed = true)
    private Integer refreshMins;
    @NacosValue(value = "${refresh.online.list.limit.switch:false}", autoRefreshed = true)
    private boolean freshOnlineListLimitSwitch;

    @Autowired
    private ChatterOnlineListMatchRecordDao chatterOnlineListMatchRecordDao;

    @NacosValue(value = "${task.job.executor.ip}", autoRefreshed = true)
    private String taskJobExecutor;

    private String localIP = NetUtil.getLocalhostStr();

    public void run(Integer mins) {

        if (!taskJobExecutor.contains(localIP)) {
            log.info("=============RefreshOnlineListLimitationTask, currentLocalAddress:{}, onlyExecuteTaskIn:{}", localIP, taskJobExecutor);
            return;
        }

        log.info("==RefreshOnlineListLimitationTask 开始执行 ==,执行间隔min:{}",mins);
        if (!freshOnlineListLimitSwitch) {
            log.info("==RefreshOnlineListLimitationTask 开关未打开 ==");
            return;
        }

        chatterOnlineListMatchRecordDao.resetTodayUseCnt();
        log.info("==RefreshOnlineListLimitationTask 执行完成 ==");
    }

    @Scheduled(fixedDelay = 1000 * 60 * 2)
    public void run2() {
        log.info("==RefreshOnlineListLimitationTask 开始2 ==");
        if (refreshMins == 2)
            run(2);
    }

    @Scheduled(fixedDelay = 1000 * 60 * 10)
    public void run10() {
        log.info("==RefreshOnlineListLimitationTask 开始10 ==");
        if (refreshMins == 10)
            run(10);
    }

    @Scheduled(fixedDelay = 1000 * 60 * 20)
    public void run20() {
        log.info("==RefreshOnlineListLimitationTask 开始20 ==");
        if (refreshMins == 20)
            run(20);
    }

    @Scheduled(fixedDelay = 1000 * 60 * 30)
    public void run30() {
        log.info("==RefreshOnlineListLimitationTask 开始30 ==");
        if (refreshMins == 30)
            run(30);
    }

    @Scheduled(fixedDelay = 1000 * 60 * 40)
    public void run40() {
        log.info("==RefreshOnlineListLimitationTask 开始 40==");
        if (refreshMins == 40)
            run(40);
    }

    @Scheduled(fixedDelay = 1000 * 60 * 50)
    public void run50() {
        log.info("==RefreshOnlineListLimitationTask 开始 50==");
        if (refreshMins == 50)
            run(50);
    }

    @Scheduled(fixedDelay = 1000 * 60 * 60)
    public void run60() {
        log.info("==RefreshOnlineListLimitationTask 开始 60==");
        if (refreshMins == 60)
            run(60);
    }

    public static void main(String[] args) {
        List<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);

        System.err.println(JsonStream.serialize(arrayList));

        JsonIterator.deserialize(JsonStream.serialize(arrayList)).asList();
        System.err.println(11);


    }
}
